import BaseAd from "../base/BaseAd";
import BaseChannel from "../base/BaseChannel";
import { FunctionType, SDKState, ADName } from "../SDKConfig";
import BaseCustomAd from "../base/BaseCustomAd";
import HitMouseC from "../../game/hitmouse/HitMouseC";

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html


export default class WXCustomAd extends BaseCustomAd {




    open(adID: string) {
        console.log('WXCustomAd this.state ', this.state, this.adUnitID)
        // if (this.state == SDKState.loadSucess) {
        //     this.show();
        // } else {
        this.load(adID)
        this.show()
        // }

    }

    load(id: string) {
        if (!id) {
            id = this.channel.getParam(0, ADName.customAd)
        }
        this.destroy();
        this.create(id)

    }

    getStyle() {

        let winSize = wx.getSystemInfoSync();
        let left = winSize.windowWidth * this.rx;
        let top = winSize.windowHeight * this.ry;
        // let left = 0
        // let top = 0;
        return {
            left: left,
            top: top,
        }
    }

    create(adID: string) {
        this.adUnitID = adID;
        if (!this.instance) {
            this.instance = wx.createCustomAd({
                adUnitId: this.adUnitID,
                adIntervals: 30,
                style: this.getStyle(),
            })
            this.instance.onLoad(this.getFunc(FunctionType.onLoad));
            this.instance.onError(this.getFunc(FunctionType.onError));
            // this.instance.onHide(this.getFunc(FunctionType.onHide));
            this.instance.onClose(this.getFunc(FunctionType.onClose));
        }
    }

    show() {
        this.setState(SDKState.open)
        if (this.instance) {
            this.instance.show();
        }
    }

    onClose() {
        console.log('WXCustomAd onClose')
    }

    onError(err) {
        console.log('WXCustomAd onError ', err)
        this.setState(SDKState.loadFail)
    }

    onHide() {
        console.log('WXCustomAd onHide')
    }

    onLoad() {
        console.log('WXCustomAd onLoad')
        this.setState(SDKState.loadSucess)
    }

    hide() {
        if (this.instance) {
            this.instance.hide();
        }
    }

    isShow() {
        if (this.instance) {
            return this.instance.isShow()
        }
        return false;
    }

    destroy() {
        if (this.instance) {
            this.instance.offLoad(this.getFunc(FunctionType.onLoad))
            this.instance.offError(this.getFunc(FunctionType.onError))
            // this.instance.offHide(this.getFunc(FunctionType.onHide))
            this.instance.offClose(this.getFunc(FunctionType.onClose))
            this.instance.destroy();
            this.instance = null;
        }
    }
}
